package com.yjt.sousou.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.yjt.sousou.R;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者：chs
 * 时间：2018-10-26 10:11
 * 描述：
 */
public abstract class BaseFragment extends Fragment {

    private Unbinder mUnbinder = null;
    private Context mContext;
    protected int mScreenWidth;
    protected abstract Object setLayoutId();
    public boolean hasLoaded = false;

    protected abstract void initView(View view, Bundle savedInstanceState);

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    public Context getActivityContext() {
        return mContext;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View rootView;
        if (setLayoutId() instanceof Integer) {
            rootView = inflater.inflate((int) setLayoutId(), container, false);
        } else if (setLayoutId() instanceof View) {
            rootView = (View) setLayoutId();
        } else {
            throw new ClassCastException("type of setLayout() must be int or View!");
        }
        mUnbinder = ButterKnife.bind(this, rootView);
        mScreenWidth = getResources().getDisplayMetrics().widthPixels;
        initView(rootView,savedInstanceState);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(!hasLoaded){
            onFirstVisible();
        }
    }

    /**
     * 在fragment首次可见时回调，可用于加载数据，防止每次进入都重复加载数据
     */
    protected void onFirstVisible() {
        hasLoaded = true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    protected void startActivity(Class clazz){
        Intent intent = new Intent(mContext,clazz);
        startActivity(intent);
    }
    /**
     * 让屏幕变暗
     */
    public void makeWindowDark() {
        if(getActivity()!=null){
            Window window = getActivity().getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 0.5f;
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            window.setAttributes(lp);
        }
    }

    /**
     * 让屏幕变亮
     */
    public void makeWindowLight() {
        if(getActivity()!=null){
            Window window = getActivity().getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 1f;
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            window.setAttributes(lp);
        }
    }

    //设置按钮样式
    protected void setState(List<TextView> textViews , int position){
        for (int i = 0; i < textViews.size(); i++) {
            if(position == i){
                textViews.get(i).setBackgroundResource(R.drawable.login_btn_bg);
                textViews.get(i).setTextColor(ContextCompat.getColor(getActivityContext(),R.color.white));
            }else {
                textViews.get(i).setBackgroundResource(R.drawable.work_state_out_bg);
                textViews.get(i).setTextColor(ContextCompat.getColor(getActivityContext(),R.color.gray_text));
            }
        }
    }

    protected void showToast(String msg){
        Toast.makeText(getActivityContext(),msg,Toast.LENGTH_SHORT).show();
    }
}
